﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XEvents.Library.Targets
{
    public class EventPayloadItem : XEventBase
    {
        internal EventPayloadItem(SqlServer server)
            : base(server)
        { }

        internal string payloaditemtype;
        internal string name;
        internal string package;
        internal string type_name;
        internal string type_package;
        internal string value;
        internal string text;

        public string PayloadItemType
        {
            get { return payloaditemtype; }
            set { }
        }

        public string Name
        {
            get { return name; }
            set { }
        }

        public string Package
        {
            get { return package; }
            set { }
        }

        public string TypeName
        {
            get { return type_name; }
            set { }
        }

        public string TypePackage
        {
            get { return type_package; }
            set { }
        }

        public string Value
        {
            get 
            {
                IEnumerable<string> mapvalue = (from m in Server.MetaData.Maps
                                                  where m.name == this.type_name
                                                  from mv in m.Values
                                                  where mv.Map_Key.ToString() == this.value
                                                  select mv.Map_Value);
                if (mapvalue.Count() == 1)
                    return mapvalue.ElementAt(0);
                else                    
                    return value; 
            }
            set { }
        }

        public string Text
        {
            get { return text; }
            set { }
        }

        public EventPayloadItem(): base(null)
        { }
    }
}
